package com.zj.leetcode.medium.greed;

/**
 * @program: algorithm
 * @description: 摆动序列
 * 376 wiggle-subsequence
 * @author: Zhang Bo
 * @create: 2022-04-11 10:14:06
 **/
public class WiggleSubsequence {
    public static void main(String[] args) {
//        int[] nums = {3, 3, 3, 2, 5};
        int[] nums = {1, 7, 4, 9, 2, 5};
//        int[] nums = {3, 3, 3};
        Solution solution = new WiggleSubsequence().new Solution();
        System.out.println(solution.wiggleMaxLength(nums));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        /**
         * nums = [1,17,5,10,13,15,10,5,16,8]
         * <p>
         * [1,7,4,9,2,5]
         *
         * @param nums
         * @return
         */
        public int wiggleMaxLength(int[] nums) {

            if (nums.length < 2) {
                return nums.length;
            }

            int flag = nums[0] - nums[1];
            int size = flag == 0 ? 1 : 2;
            for (int i = 2; i < nums.length; i++) {
                int temp = nums[i - 1] - nums[i];
                if (flag >= 0 && temp < 0 || flag <= 0 && temp > 0) {
                    size++;
                    flag = temp;
                } else if (flag == 0) {
                    flag = temp;
                }

            }

            return size;

        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
